<?php
/**
 * Session and User (database) handler for OpenSSO PHP library.
 * 
 * This class's functions connect your application with the OpenSSO PHP library.
 * Please read the inline documentation and write this class!
 * Before you test it please look after the config file: config.php!
 *
 * API Abstract version: 1.5.1
 *  
 * @category   OpenSSO_PHP
 * @package    OpenSSO_PHP
 * @link       http://kir-dev.sch.bme.hu/opensso-phplib/
 * @filesource
 */
 
require_once('open-sso.abstract.php');

final class openSSO extends openSSO_Abstract
{

	/**
	 * Get data from the session
	 *
	 * @param (string) $name Name of the data.
	 * @return (multi) Value of the data.
	 */
	protected function getSessionData($name)
	{
            return Session::instance()->get($name);
	}
 
 
 
	/**
	 * Set data to the session.
	 * Set the '$value' data to the session with '$name' name.
	 * If this name exists overwrite it.
	 * If this name don't exists create it.
	 *
	 * @param (string) $name Name of the data.
	 * @param (multi) $value Value of the data.
	 * @return void
	 */
	protected function setSessionData($name, $value)
	{
            Session::instance()->set($name, $value);
	}
 
 
 
	/**
	 * Create user.
	 * Create user with data from the '$data' param (user's SSO data).
	 * Insert to DB or whatever you store the users.
	 * Syntax: The data name that you setted in config file ('user' array) with 'sso_' prefix.
	 *	        Plus 'user_name', 'user_virid', 'user_groups' variables, and these with 'sso_' prefix.
	 *
	 * @param (array) $data Single array of the user's SSO data. Keys represents the data name.
	 * @return void
	 */
	protected function insertUser($data)
	{
            /*  
                REMOTE_USER                felhasználói név                 user 
                HTTP_UID                   felhasználói név(= REMOTE_USER)  user 
                HTTP_EMAIL                 e-mail cím                       user@gmail.com 
                HTTP_COMMON_NAME           teljes név                       Kis József 
                HTTP_SURNAME               vezetéknév                       Kis 
                HTTP_GIVEN_NAME            keresztnév                       József 
                HTTP_NICKNAME              becenév                          józsika 
                HTTP_VIRID                 VIR ID, ha van neki              lásd lejjebb 
                HTTP_EDUPERSONENTITLEMENT  csoporttagságok                  lásd lejjebb
             */
	}
 
 
 
	/**
	 * Get the user's data.
	 * Select user where the user name is equal with the '$userName' param.
	 * Read from DB or whatever you store the users.
	 * Return with all of the data that you store to this user (SSO and none SSO data).
	 * You must return the data names that the program add when the data stored (insert, update)!
	 *
	 * @param (string) $userName The unique user name.
	 * @return (array) Single array of the user's data (SSO and none SSO). Keys represents the data name.
	 */
	protected function selectUser($userName)
	{
            return ORM::factory('users')->where('username', '=', $userName)->find();
	}
 
 
 
	/**
	 * Update user's data.
	 * Update all (SSO and none SSO) user's data where the user name is equal with the '$userName' param.
	 * Update in DB or whatever you store the users.
	 * '$data' syntax: The data name that you setted in config file with 'sso_' prefix.
	 *                 Plus 'user_name', 'user_virid', 'user_groups' variables, and these with 'sso_' prefix.
	 *                 Plus your other data that added with 'setUserData();' function.
	 *
	 * @param (string) $userName The unique user name.
	 * @param (array) $data Single array of the user's data (SSO and none SSO). Keys represents the data name.
	 * @return void
	 */
	protected function updateUser($userName, $data)
	{   
            $post='';
            $s = $data['HTTP_EDUPERSONENTITLEMENT'];
            $s = explode('<', $s);

            foreach($s as $key => $value){
                $value = trim($value, '>[]|');
                $s[$key] = explode(':', $value);
            }
            
            foreach($s as $key => $value){
                if(strpos($value[5], 'AC Studio & Live')!=false){
                    $post = $value[5];
                }
            }
            
            if($post!=''){
                $user = ORM::factory('users')->where('username', '=', $userName)->find();
                $user->username = $userName;
                $user->email = $data['HTTP_EMAIL'];
                $user->name = $data['HTTP_COMMON_NAME'];
                $user->post = $post;
                $user->save();
            }
	}
 
 
 
	/**
	 * Hooks (optional)
	 */	 
 
 	/**
	 * Hook: Login
	 * Call this function after the lib checked the required user data, and created the session.
	 * (Call this function after the login method.)	 
	 *
	 * @param void
	 * @return void
	 */
	protected function hookLogin()
	{
	}
	
	
	
	/**
	 * Hook: Logout
	 * Call this function after the lib "destroy" the session.
	 * (Call this function after the logout method.)	 
	 *
	 * @param void
	 * @return void
	 */
	protected function hookLogout()
	{
	}



	/**
	 * Hook: Is Error?
	 * Call this function to determine that error is occured or not.
	 *
	 * @param void
	 * @return (bool) 'TRUE' if error is occured and 'FALSE' otherwise.
	 */
	protected function hookIsError()
	{
	}


	
	/**
	 * Hook: Error
	 * Call this function when the IDP or the SP is down.
	 *
	 * @param void
	 * @return void
	 */
	protected function hookError()
	{
	}
 
	/**
	 * End of Hooks
	 */	 	
 
 
 
}

?>
